package com.example.springbootweb_comprehensiveexample.controller;

import com.example.springbootweb_comprehensiveexample.anno.Log;
import com.example.springbootweb_comprehensiveexample.pojo.Dept;
import com.example.springbootweb_comprehensiveexample.pojo.Result;
import com.example.springbootweb_comprehensiveexample.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class DeptController {

    //private static Logger log = LoggerFactory.getLogger(DeptController.class);//可通过注解@Slf4j来代替

    @Autowired
    private DeptService deptService;

    /**
     * 查询部门数据
     * @return
     */
    //@RequestMapping(value = "/depts", method = RequestMethod.GET)  //限定请求方式为GET
    @GetMapping("/depts")  //是上面一行代码的简写
    public Result list(){
        //System.out.println("查询全部部门信息");   //用log来输出的到控制台，不用sout
        log.info("查询全部部门信息");

        //调用service层的方法，查询全部部门信息
    List<Dept> deptList = deptService.list();

        return Result.success(deptList);
    }

    /**
     * 删除部门数据
     *
     * @return
     */
    @Log
    @DeleteMapping("/depts/{id}")
    public Result delete(@PathVariable Integer id) throws Exception {
        log.info("删除部门：{}", id);
        deptService.delete(id);
        return Result.success();
    }

    /**
     * 添加部门
     */
    @Log
    @PostMapping("/depts")
    public Result add(@RequestBody Dept dept){
        log.info("添加部门：{}", dept);
        deptService.add(dept);
        return Result.success();
    }

    //更新部门
      //1.根据id查询部门
    @Log
    @GetMapping("/depts/{id}")
    public Result selectById(@PathVariable Integer id){
        Dept dept = deptService.selectById(id);
        return Result.success(dept);
    }
      //2.修改部门信息
    @Log
    @PutMapping("/depts")
    public Result update(@RequestBody Dept dept){
        deptService.update(dept);
        return Result.success();
    }

}
